/// <reference path="../interfaces/IUserDetails.ts" />
/// <reference path="../../../../typings/linq/linq.d.ts" />
module Sustainalytics.Accounts {


    interface IUserDetailsController extends ng.IScope {
        getUser: any;
        userDetails: UsersDto;
        formatDate: any;
        resetPassword: any;
    }

    class UserDetailsController {
        constructor(private $scope: IUserDetailsController, $rootScope, $routeParams, notificationFactory: INotificationFactory, staticObjects: IStaticObjects, userFactory : UsersService) {
            var userId = $routeParams.userId;

            $scope.getUser = () => {
                userFactory.userGet(userId)
                    .then(user => {
                        $scope.userDetails = user;
                        $scope.userDetails.passwordExpirationDate = new Date(user.passwordExpirationDate.toString());
                    });
            };

            $scope.resetPassword = () => {
                var listOfIds = [$scope.userDetails.id.toString()];
                if (confirm("The password will be reset for this user."
                    + "\n" + "Are you sure that you want to continue?")
                    ) {
                    userFactory.usersResetPasswords(listOfIds)
                        .then(result=> {
                            notificationFactory.success("The password has been reset for 1 user.");
                            });
                }
            }

        $.when($scope.getUser());
        }
    }

    appRoot.controller('UserDetailsController', ['$scope', '$rootScope', '$routeParams', 'notificationFactory', 'staticObjects', 'UsersService', ($scope, $rootScope, $routeParams, notificationFactory, staticObjects, userFactory) =>
        new UserDetailsController($scope, $rootScope, $routeParams, notificationFactory, staticObjects, userFactory)
    ]);
}